package com.ly.hotel.spainterface.pp.view;

import android.content.Context;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.ly.hotel.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by ThinkPad on 2018/4/2.
 */

public class CommentCustomToast {

    private static Toast mToast;
    private Handler handler = new Handler();

    private CommentCustomToast(Context context, CharSequence text, int duration, boolean type) {
        View view;
        if (type) {
            view = LayoutInflater.from(context).inflate(R.layout.comment_custom_success_toast_layout, null);
        } else {
            view = LayoutInflater.from(context).inflate(R.layout.comment_custom_failed_toast_layout, null);
        }
        TextView textView = view.findViewById(R.id.toast_message);
        textView.setText(text);
        if (mToast != null) {
            mToast.cancel();
        }
        mToast = new Toast(context);
        mToast.setDuration(duration);
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.setView(view);
    }

    public static CommentCustomToast makeText(Context context, CharSequence text, int duration, boolean type) {
        return new CommentCustomToast(context, text, duration, type);
    }

    // 正常的消失时间
    public void show() {
        if (mToast != null) {
            mToast.show();
        }
    }

    public void setGravity(int gravity, int xOffset, int yOffset) {
        if (mToast != null) {
            mToast.setGravity(gravity, xOffset, yOffset);
        }
    }

    // 可以自定义消失时间
    public void showMyToast(final int duration) {
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                mToast.show();
            }
        }, 0, Toast.LENGTH_LONG);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                mToast.cancel();
                timer.cancel();
            }
        }, duration);
    }
}

